{include file="public/layout" /}
<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 
<span class="c-gray en">&gt;</span> 商品管理
<span class="c-gray en">&gt;</span> 添加商品
<a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="Hui-iconfont">&#xe68f;</i>
</a>
</nav>
<div class="page-container" style="padding-top: 3rem;">
    <form action="/Admin/goods/save" method="post" class="form form-horizontal" id="form-article-add">
         <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>宝贝链接：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" placeholder="输入第三方平台链接URL" id="http_link" name="http_link" value="{$goods_info.click_url ? $goods_info.click_url : ''}"  style="width: 80%;">
                <button onClick="getGoodsInfo();" class="btn btn-secondary radius" type="button">一键采集</button>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>淘宝IID：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="{$goods_info.num_iid ? $goods_info.num_iid : ''}" id="num_iid" name="num_iid" style="width: 80%;">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>商品标题：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="{$goods_info.title ?  $goods_info.title : ''}" id="title" name="title" style="width: 80%;">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>商品图片：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="{$goods_info.pic_url ?  $goods_info.pic_url : ''}" id="pic_url" name="pic_url" style="width: 80%;">
                <button class="btn btn-secondary radius" type="button" onClick="GetUploadify(1,'','pic_url','img_call_back')">上传</button>
            </div>
        </div>
        <div class="row cl">
            <div style="margin-left: 19rem;">
                {if isset($goods_info['pic_url'])}
                <img src="{$goods_info.pic_url}" id="goods_img" width="100" height="100">
                {else / }
                <img  id="goods_img" width="100" height="100" style="display: none;">
                {/if}
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>销量：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="{$goods_info.volume ? $goods_info.volume : ''}" id="volume" name="volume" style="width: 80%;">
            </div>
        </div>
         <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>原价(元)：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="{$goods_info.price ? $goods_info.price : ''}" id="price" name="price" style="width: 80%;">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>折扣价(元) ：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="{$goods_info.discount_price ?  $goods_info.discount_price : ''}" id="discount_price" name="discount_price" style="width: 80%;">
            </div>
        </div>
         <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>商品来源：</label>
            <div class="formControls col-xs-8 col-sm-9"> <span class="select-box" style="width: 80%;">
                <select name="shop_type" class="select">
                    <option value="C" {if isset($goods_info.shop_type) && $goods_info.shop_type == 'C'}selected{/if}>淘宝</option>
                    <option value="T" {if isset($goods_info.shop_type) &&  $goods_info.shop_type == 'T'}selected{/if}>天猫</option>
                    <option value="D" {if isset($goods_info.shop_type) &&  $goods_info.shop_type == 'D'}selected{/if}>京东</option>
                    <option value="P" {if isset($goods_info.shop_type) &&  $goods_info.shop_type == 'P'}selected{/if}>拼多多</option>
                </select>
                </span> 
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2">推广链接：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="click_url" id="click_url"  value="{$goods_info.click_url ?  $goods_info.click_url : ''}" class="input-text" style="width: 80%;">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2">佣金：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="commission" id="commission" value="{$goods_info.commission ?  $goods_info.commission :''}" class="input-text" style="width: 80%;">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2">佣金比率：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="commission_rate" id="commission_rate"  value="{$goods_info.commission_rate ? $goods_info.commission_rate : ''}" class="input-text" style="width: 80%;">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2">所属分类：</label>
            <div class="formControls col-xs-8 col-sm-9"> <span class="select-box" style="width: 80%;">
                <select class="small form-control" style="height: 30px;" onChange="get_category(this.value,'cat_id_2','0');">
                    <option value="0">请选择商品分类</option>
                    {if isset($cat_list)}
                        {foreach name="cat_list" item="v" key="k"}
                            <option value="{$v['id']}" {if isset($level_cat[1]) && $level_cat[1] == $v['id']} selected="selected" {/if}>
                            {$v['name']}
                            </option>
                        {/foreach}
                    {/if}
                </select>&nbsp;&nbsp;
                {if isset($level_cat[2])}
                <select id="cat_id_2" onChange="get_category(this.value,'cat_id_3','0');" class="small form-control" style="height: 30px;">
                        <option value="{$level_cat[2]}" selected="selected">{$arr_cat['cat2'][$level_cat[2]]}</option>
                </select>&nbsp;&nbsp;
                {else /}
                <select id="cat_id_2" onChange="get_category(this.value,'cat_id_3','0');" class="small form-control" style="height: 30px; display: none;">
                        <option value="0">请选择商品分类</option>
                </select>&nbsp;&nbsp;
                {/if}
                 {if isset($level_cat[3])}
                 <select id="cat_id_3" class="small form-control" style="height: 30px;" onChange="get_category(this.value,'cat_id_4','0');">
                         <option value="{$level_cat[3]}" selected="selected">{$arr_cat['cat3'][$level_cat[3]]}</option>
                 </select>
                 {else /}
                 <select id="cat_id_3" class="small form-control" style="height: 30px;display: none;" onChange="get_category(this.value,'cat_id_4','0');">
                        <option value="0">请选择商品分类</option>
                  </select>
                 {/if}
                </span>
                <input type="hidden" name="cate_id" id="cate_id" value="{$goods_info.cate_id ? $goods_info.cate_id : ''}">
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                <input type="hidden" name="goods_id" value="{$goods_info.goods_id ?  $goods_info.goods_id : ''}">
                <input type="hidden" name="small_images" id="small_images" value="{$goods_info.small_images ?  $goods_info.small_images :''}">
                <button  class="btn btn-primary radius" type="submit"><i class="Hui-iconfont">&#xe632;</i> 保存并提交</button>
                <button  class="btn btn-default radius" type="reset">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
            </div>
        </div>
    </form>
</div>
{include file="public/footer" /}
<script type="text/javascript">
   function getGoodsInfo() {
        var regUrl = /(.*\.?taobao.com(\/|$))|(.*\.?tmall.com(\/|$))|(.*\.?jd.com(\/|$))|(.*\.?yangkeduo.com(\/|$))/i;      
        var http_link = $("#http_link").val();
        if(http_link == ''){
            layer.msg('URL不能为空！',{icon:2,time:1000});       
            return false;
        }
        if (!http_link.match(regUrl)){
            layer.msg('请输入正确URL！',{icon:2,time:1000});       
            return false;
        }

        $.ajax({
            type: 'POST',
            url: '{:U("/Admin/goods/getTaboaGoods")}',
            data:{http_link: http_link},
            dataType: 'json',
            success: function(data){
                //成功返回数据
                if(data.status == 1) {
                    var small_images = data.info.small_images;
                    var small_img = small_images.join('##');
                    $("#num_iid").val(data['info']['num_iid']);
                    $("#title").val(data.info.title_name);
                    $("#pic_url").val(data.info.pict_url);
                    $("#goods_img").attr("src",data.info.pict_url).css("display","block");
                    $("#volume").val(data.info.volume);
                    $("#price").val(data.info.price);
                    $("#discount_price").val(data.info.discount_price);
                    $("#small_images").val(small_img);
                } else {
                    layer.msg(data.error_info,{icon:2,time:1000});
                }
            },
            error:function(data) {
                console.log(data.msg);
            },
        }); 
   }

/**
 * 获取多级联动的商品分类
 */
function get_category(id,next,select_id){
    if(id == 0){
        var html = "<option value='0'>请选择商品分类</option>";
        $('#'+next).empty().html(html);
        return false;
    }

    $.ajax({
        type : "GET",
        url  : '/Admin/goods/get_category?parent_id='+ id,
        dataType:'json',
        success: function(data) {
            var html = "<option value='0'>请选择商品分类</option>";
            if(data.status == 1){
                for (var i=0 ;i<data.result.length;i++){
                    html+= "<option value='"+data.result[i].id+"'>"+data.result[i].name+"</option>";
                }
            }
            (next == 'cat_id_2') && $("#cat_id_3").hide(); 
            (data.result.length > 0 ) && $('#'+next).show();
            $('#'+next).empty().html(html);
            (select_id > 0) && $('#'+next).val(select_id);//默认选中
        }
    });
    $("#cate_id").val(id);
}

function img_call_back(fileurl_tmp)
{
      $("#pic_url").val(fileurl_tmp);
      $("#goods_img").attr('src', fileurl_tmp).show();
}
</script>
</body>
</html>